#!/bin/bash

IPList=(112.238.204.1 180.88.16.84 119.112.108.1 58.60.3.5 58.58.20.160 58.242.176.254 218.19.223.180 222.174.210.1)
PINGnum=50
TIME=`date +%H:%M`
LOGFILE=/data/pingtest/log/`date +%Y%m%d`.csv
IPnum=${#IPList[@]}
TEMP="date,"
if [ ! -s $LOGFILE ] 
then for ((i=0;i<$IPnum;i++));
	do
	TEMP=$TEMP${IPList[i]}",,,,"
	TEMP2=$TEMP2",min,avg,max,loss"
	done
	echo $TEMP >> $LOGFILE
	echo $TEMP2 >> $LOGFILE
fi

OUT=$TIME

for ((i=0;i<$IPnum;i++));
do
	ping -c $PINGnum ${IPList[i]} > /tmp/ping.txt
	if [ $? == 1 ]
	then echo rtt min/avg/max/mdev = 0/0/0/0 ms >> /tmp/ping.txt
	fi
	DELAY=`cat /tmp/ping.txt |grep rtt |awk '{print $4}'|awk -F\/ '{print ","$1","$2","$3","}'`
	LOSS=`cat /tmp/ping.txt |grep loss|awk '{print $6}'`
	OUT=$OUT$DELAY$LOSS
done

echo $OUT >>$LOGFILE
